package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DcitFeignClient;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaowei
 * @description:
 * @create 2021-12-12-16:52
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DcitFeignClient dcitFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
//        把参数map集合转换为对象
        String s = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);
//        判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital isExist = hospitalRepository.getHospitalByHoscode(hoscode);


//        如果存储,进行修改
        if (isExist != null) {
            hospital.setStatus(isExist.getStatus());
            hospital.setCreateTime(isExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else { //        如果不存在，进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    //医院列表(条件查询带分页)
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
//        创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
//        创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
//        HospitalSetQueryVo转换为Hospital对象
        Hospital hospital = new Hospital();
//        创建对象
        Example<Hospital> example = Example.of(hospital, matcher);
//        调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
//        获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item ->{
            this.setHospitalHosType(item);
        });
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
//        根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
//        设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }
//医院详细信息
    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String, Object> result = new HashMap<>();
        Hospital hospital = hospitalRepository.findById(id).get();
        Hospital hospital1 = this.setHospitalHosType(hospital);
//        医院基本信息，包含等级
        result.put("hospital",hospital1);
        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    private Hospital setHospitalHosType(Hospital hospital) {
//        根据dictcode和value获取医院等级名称
        String hostypeString = dcitFeignClient.getName("Hostype", hospital.getHostype());
//        查询省市区
        String provinceString = dcitFeignClient.getName(hospital.getProvinceCode());
        String cityString = dcitFeignClient.getName(hospital.getCityCode());
        String districtString = dcitFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        return hospital;
    }
}